/*
 *  $TEAM_NAME Space Trader
 */
package models;


import java.util.Collection;
import java.util.Map;
import java.util.Hashtable;
import java.util.Observable;

/**
 * The class marketplace represents a marketplace withint a game
 * @author bradley
 * @version 1.0
 */
public class Marketplace extends Observable {
	/**
	 * field
	 */
	private Map<String, Integer> quantity;
	
	/**
	 * field
	 */
	private Map<String, Integer> prices;
	
	/**
	 * No arg constructor for marketplace
	 */
	public Marketplace() {
		this.quantity = null;
	}
	
    /**
     * Constructor - makes a new market with conditions present
     *  in system.
     *  @param ss
     */
    public Marketplace(StarSystem ss) {
        quantity = new Hashtable<String, Integer>();
        prices = new Hashtable<String, Integer>();

        for (TradeGood good : TradeGood.getTradeGoods()) {
            quantity.put(good.getName(), good.getQuantity(ss));
            prices.put(good.getName(), good.getPrice(ss));
        }
    }

    /**
     * Buys a trade good
     * @param good - the good
     * @param quantity - the quantity to buy
     * @return - true if the buy goes through
     */
    public boolean canBuy(TradeGood good, int quantity) {
        final int available = getQuantity(good);

        if (available < quantity) {
            return false;
        }

        setQuantity(good, available - quantity);
        
        setChanged();
        return true;
    }

    /**
     * Gets the price of the trade good
     * @param good - the trade good
     * @return the price
     */
    public int getPrice(TradeGood good) {
        return prices.get(good.getName());
    }

    /**
     * Gets the quantities
     * @return the quantities
     */
    public Map<String, Integer> getQuantity() {
		return quantity;
	}

    /**
     * Gets the quantity of a trade good
     * @param good - the trade good
     * @return the quantity
     */
    public int getQuantity(TradeGood good) {
    	return quantity.get(good.getName());
    }
   
    /**
     * Gets the collection trade goods
     * @return the collection of goods
     */
    public Collection<TradeGood> getTradeGoods() {
        return TradeGood.getTradeGoods();
    }

    /**
     * Sells a number of trade goods
     * @param good - the type of good to sell
     * @param quantity - the quantity to sell
     * @return true if the transaction went through
     */
    public boolean canSell(TradeGood good, int quantity) {
        final int available = getQuantity(good);

        setQuantity(good, available + quantity);

        setChanged();
        return true;
    }

    /**
     * Sets the price of a trade good
     * @param good - the Trade good
     * @param p - the price
     */
	public void setPrice(TradeGood good, int p) {
        prices.put(good.getName(), p);
        setChanged();
    }

	/**
	 * Sets the quantities
	 * @param quantity - the quantities
	 */
	public void setQuantity(Map<String, Integer> quantity) {
		this.quantity = quantity;
	}
	
	/**
	 * Sets the quantity of a trade good
	 * @param good - the trade good
	 * @param q - the quantity
	 */
    public void setQuantity(TradeGood good, int q) {
        quantity.put(good.getName(), q);
        setChanged();
    }
    
    /**
     * to string
     * @return string
     */
    public String toString() {
		return "";
	}

	/**
	 * @return the prices
	 */
	public Map<String, Integer> getPrices() {
		return prices;
	}

	/**
	 * @param prices the prices to set
	 */
	public void setPrices(Map<String, Integer> prices) {
		this.prices = prices;
	}
}
